package com.xue.controller;

import com.xue.annotation.LogOperation;
import com.xue.pojo.Dept;
import com.xue.pojo.Result;
import com.xue.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    DeptService deptService;


    /*
     * 查询所有部门
     * */
    @LogOperation
    @GetMapping
    public Result getAllDept() {
        log.info("查询部门列表");
        List<Dept> list = deptService.getAllDept();
        return Result.success(list);
    }

    /*
     * 根据部门id来删除部门
     * */
    @LogOperation
    @DeleteMapping
    public Result deleteDeptById(Integer id) {
        log.info("根据id删除部门, id: {}", id);
        deptService.deleteDeptById(id);
        return Result.success();
    }


    /*
     * 添加部门
     * */
    @LogOperation
    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        log.info("新增部门, dept: {}", dept);
        deptService.addDept(dept);
        return Result.success();
    }

    /*
     * 根据id来查询部门
     * */
    @LogOperation
    @GetMapping("/{id}")
    public Result getDeptById(@PathVariable Integer id) {
        log.info("根据ID查询, id: {}", id);
        Dept dept = deptService.getDeptById(id);
        return Result.success(dept);
    }

    /*
     * 修改部门
     * */
    @LogOperation
    @PutMapping
    public Result updateDept(@RequestBody Dept dept) {
        log.info("修改部门, dept: {}", dept);
        deptService.updateDept(dept);
        return Result.success();
    }


}
